import subprocess
from pathlib import Path
from core.backup import BackupManager
from plugins.template.adapter import PluginBase

class NixOSAdapter(PluginBase):
    def __init__(self):
        self.channel_path = Path(f"{os.environ['HOME']}/.nix-channels")
        self.backup = BackupManager("nixos")
    
    @classmethod
    def is_compatible(cls) -> bool:
        return NixOSDetector().detect()
    
    def apply_mirror(self, mirror_url: str):
        self.backup.create(self.channel_path)
        
        # 更新官方频道
        subprocess.run([
            "nix-channel", "--add", 
            f"{mirror_url}/nix-channels/nixpkgs-unstable",
            "nixpkgs"
        ], check=True)
        subprocess.run(["nix-channel", "--update"], check=True)